package com.probox.modules.controller.customer;

import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.customer.CustomerGroup;
import com.probox.common.entity.service.customer.ICustomerGroupDefinitionService;
import com.probox.common.entity.service.customer.ICustomerGroupService;
import com.probox.common.entity.vo.customer.CustomerGroupVo;
import com.probox.common.entity.vo.customer.GroupDefinitionInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 客户分群Controller
 *
 * @author zsy
 * @date 2021-07-15
 */
@RestController
@RequestMapping("/customer/CustomerGroup")
public class CustomerGroupController extends BaseController {
    @Autowired
    private ICustomerGroupService customerGroupService;

    @Autowired
    private ICustomerGroupDefinitionService customerGroupDefinitionService;

    /**
     * 查询客户分群列表
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerGroupVo customerGroupvo) {
        startPage();
//        List<CustomerGroup> list = customerGroupService.queryList(customerGroup);
        List<CustomerGroupVo> list = customerGroupService.selectGroupInfo(customerGroupvo);
        return getDataTable(list);
    }


    /**
     * 获取客户分群详细信息
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:query")
    @GetMapping(value = "/{groupUid}")
    public AjaxResult getInfo(@PathVariable("groupUid") Long groupUid) {
        return AjaxResult.success(customerGroupService.selectCustomerGroupById(groupUid));
    }

    /**
     * 新增客户分群
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:add")
    @Log(title = "客户分群", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerGroup customerGroup) {
        return toAjax(customerGroupService.insertCustomerGroup(customerGroup));
    }

    /**
     * 修改客户分群
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:edit")
    @Log(title = "客户分群", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerGroup customerGroup) {
        return toAjax(customerGroupService.updateCustomerGroup(customerGroup));
    }

    /**
     * 删除客户分群
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:remove")
    @Log(title = "客户分群", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupUids}")
    public AjaxResult remove(@PathVariable Long[] groupUids) {
        return toAjax(customerGroupService.deleteCustomerGroupByIds(groupUids));
    }

    /**
     * 根据条件查询客户信息
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:query")
    @PostMapping(value = "/selectGroupCustomerInfo")
    public List selectGroupCustomerInfo(@RequestBody List<GroupDefinitionInfoQuery> groupDefinitionInfoQuerie){
        List customerUid = customerGroupDefinitionService.selectConditionCustomerInfo(groupDefinitionInfoQuerie);
        return customerUid;
    }

    /**
     * 新增人群分组
     * @param customerGroupVo
     * @return
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:add")
    @Log(title = "客户分群", businessType = BusinessType.INSERT)
    @PostMapping("/addGroup")
    public AjaxResult addGroup(@RequestBody CustomerGroupVo customerGroupVo) {
        return toAjax(customerGroupService.addGroup(customerGroupVo));
    }

    /**
     * 修改人群分组
     * @param customerGroupVo
     * @return
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:edit")
    @Log(title = "客户分群", businessType = BusinessType.UPDATE)
    @PostMapping("/editGroup")
    public AjaxResult editGroup(@RequestBody CustomerGroupVo customerGroupVo) {
        return toAjax(customerGroupService.editGroup(customerGroupVo));
    }

    /**
     * 查看分群名称简介列表
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:list")
    @GetMapping("/getGroupInfo")
    public TableDataInfo getGroupInfo() {
        startPage();
        List<CustomerGroup> list = customerGroupService.queryList();
        return getDataTable(list);
    }

    /**
     * 查看指定分群名称及人数
     */
    @PreAuthorize(hasPermi = "group:CustomerGroup:list")
    @GetMapping("/getGroupInfoById/{groupUid}")
    public AjaxResult getGroupInfoById(@PathVariable Long groupUid) {
        return AjaxResult.success(customerGroupService.getGroupInfoById(groupUid));
    }

}
